package com.sxp.dao.impl;

import com.sxp.dao.BookDao;
import com.sxp.bean.Book;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 宋晓盼
 * @since 2019/6/8 22:28
 */
public class BookDaoImpl implements BookDao {

    private SessionFactory factory = new Configuration().configure().buildSessionFactory();

    @Override
    public List<Book> listByName(String name) {
        String hql = "from Book as book where book.name like :name";
        List<Book> books = new ArrayList<>();

        try(Session session = factory.openSession()) {
            Query query = session.createQuery(hql);
            query.setParameter("name", "%" + name + "%");
            List result = query.list();
            if (result.size() != 0) {
                for (Object book: result) {
                    books.add((Book) book);
                }
            }
        }catch (HibernateException e) {
            e.printStackTrace();
        }
        return books;
    }

    @Override
    public Integer save(Book book) {
        Integer bookId = null;
        Transaction transaction = null;
        try(Session session = factory.openSession()) {
            transaction = session.beginTransaction();
            bookId = (Integer) session.save(book);
            transaction.commit();
            return bookId;
        } catch (HibernateException e){
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
        return bookId;
    }

    @Override
    public List<Book> list(int limit) {
        String hql = "from Book";
        List<Book> books = new ArrayList<>();
        try(Session session = factory.openSession()) {
            Query query = session.createQuery(hql);
            if (limit > 0) {
                query.setMaxResults(limit);
            }
            List result = query.list();
            for (Object book: result) {
                books.add((Book) book);
            }
        }
        return books;
    }

    @Override
    public Book getById(int id) {
        String hql = "from Book as book where id = :id";
        try(Session session = factory.openSession()) {
            Query query = session.createQuery(hql);
            query.setParameter("id", id);
            List result = query.list();
            if (result.size() == 1) {
                return (Book) result.get(0);
            } else {
                return null;
            }
        }
    }

    @Override
    public void update(Book book) {
        Transaction transaction = null;
        try (Session session = factory.openSession()) {
            transaction = session.beginTransaction();
            session.update(book);
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Book> listByClickCountDesc(int limit) {
        String hql = "from Book as book order by book.clickCount desc";
        List<Book> books = new ArrayList<>();
        try(Session session = factory.openSession()) {
            Query query = session.createQuery(hql);
            query.setMaxResults(limit);
            List result = query.list();
            for (Object book: result) {
                books.add((Book) book);
            }
        }
        return books;
    }

    @Override
    public void deleteById(int id) {
        Transaction transaction = null;
        try(Session session = factory.openSession()) {
            transaction = session.beginTransaction();
            Book book = session.get(Book.class, id);
            session.delete(book);
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }
    }


}
